package com.yuqing.sentimentapp.workspace.search.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.linkface.baselibrary.utils.DateUtil;
import com.linkface.baselibrary.utils.DensityUtil;
import com.linkface.common.adaper.CommonRecycleAdapter;
import com.linkface.common.adaper.CommonViewHolder;
import com.linkface.common.adaper.MultiTypeSupport;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.utils.DisplayUtil;
import com.yuqing.sentimentapp.utils.GlideUtil;
import com.yuqing.sentimentapp.widget.jzvideo.Jzvd;
import com.yuqing.sentimentapp.widget.jzvideo.JzvdStd;
import com.yuqing.sentimentapp.workspace.search.entity.YqSearchResult;
import com.yuqing.sentimentapp.yqlist.bean.YuQingDataBean;

import java.util.List;

import wseemann.media.FFmpegMediaMetadataRetriever;

/**
 * Created by shenhe on 2018/9/19.
 */

public class YqSearchAdapter extends CommonRecycleAdapter<YqSearchResult.DataBean.ListBean> implements MultiTypeSupport<YqSearchResult.DataBean.ListBean> {
    private CommonViewHolder.onItemCommonClickListener commonClickListener;
    private Context mContext;

    public YqSearchAdapter(Context context, List<YqSearchResult.DataBean.ListBean> dataList) {
        super(context, dataList, R.layout.item_yq_search_no_pic);
        this.mContext = context;
    }

    public YqSearchAdapter(Context context, List<YqSearchResult.DataBean.ListBean> dataList, CommonViewHolder.onItemCommonClickListener commonClickListener) {
        super(context, dataList, R.layout.item_yq_search_no_pic);
        this.mContext = context;
        multiTypeSupport = this;
        this.commonClickListener = commonClickListener;
    }

    @Override
    public void bindData(final CommonViewHolder holder, final YqSearchResult.DataBean.ListBean data) {
        String caption = data.getCaption();
        if (TextUtils.isEmpty(caption)) caption = "";
        String dataTime = "";
        try {
            dataTime = DateUtil.formatDateTime(data.getDatatime());
        } catch (Exception e) {
            dataTime = "";
        }
        holder.setText(R.id.tv_title,caption + data.getTitle())
        .setText(R.id.tv_source, data.getSource())
        .setText(R.id.tv_date_time, dataTime);

        String imageUrl = data.getImageUrl();
        if (!TextUtils.isEmpty(imageUrl)) {
            String[] images = imageUrl.split(";");
            if (images.length > 0 && images.length < 3){//一张图
                GlideUtil.loadRoundImage(mContext, images[0], holder.getView(R.id.iv_pic), 3);
            } else if (images.length >= 3){//三张图
                GlideUtil.loadRoundImage(mContext, images[0], holder.getView(R.id.iv_pic_one), 3);
                GlideUtil.loadRoundImage(mContext, images[1], holder.getView(R.id.iv_pic_two), 3);
                GlideUtil.loadRoundImage(mContext, images[2], holder.getView(R.id.iv_pic_three), 3);
            }
        }
        holder.setCommonClickListener(commonClickListener);
        holder.getView(R.id.tv_source_link).setOnClickListener(v -> {
            String url = data.getUrl();
            if (!TextUtils.isEmpty(url)) {
                //跳转到默认浏览器
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getLayoutId(YqSearchResult.DataBean.ListBean item, int position) {
        String imageUrl = item.getImageUrl();
        if (TextUtils.isEmpty(imageUrl)) {
            return R.layout.item_yq_search_no_pic;
        }
        String[] images = imageUrl.split(";");
        if (images.length == 0) {
            return R.layout.item_yq_search_no_pic;
        } else if (images.length < 3){
            return R.layout.item_yq_search_one_pic;
        } else {
            return R.layout.item_yq_search_three_pic;
        }
    }

}
